---@class GT.ui.control.item : GT.ui
---@field u table<RoleID,GT.ui.control.item> 玩家独立数据驱动
---@field binding GT.ui.control.item
---@field item_enter_ck UI_EVENT_item 进入
---@field item_leave_ck UI_EVENT_item 离开
---@field cast_key KeyboardKey 智能施法快捷键
---@field key KeyboardKey 快捷键
GT.ui.control.item = class(GT.ui, false)

---@alias UI_EVENT_item fun(data:any,p:GT.player,ui:GT.ui.control.item):void

---@class slot_binding 绑定物品栏
---@field slot_type SlotType 槽位类型
---@field slot_index number 槽位ID 0开始
---@field u GT.unit 绑定单位



---@class ui_data_item : ui_data
---@field slot_binding slot_binding 类型
---@field item_enter_ck UI_EVENT_item 进入
---@field item_leave_ck UI_EVENT_item 离开
---@field cast_key KeyboardKey 智能施法快捷键
---@field key KeyboardKey 快捷键

--- 创建物品组件
---@param data ui_data_item
---@return GT.ui.control.item
function GT.ui.control.item:ctor(data)
    data.class = GT.ui.control.item
    if data.uid ~= nil then
        super(self, data)
    else
        data.u_type = GT.ui.type.item
        self.slot_binding = data.slot_binding
        self.item_enter_ck = data.item_enter_ck
        self.item_leave_ck = data.item_leave_ck
        self.cast_key = data.cast_key
        self.skill = data.skill
        --创建控件
        super(self, data)

        if self.slot_binding ~= nil then
            self:set_ui_unit_slot(self.slot_binding)
        end
        if self.cast_key ~= nil then
            self:set_skill_btn_smart_cast_key(self.cast_key)
        end
        if self.key ~= nil then
            self:set_key(self.key)
        end
    end

    self["背景"] = GT.ui.control.image.obj({
        player = self.player,
        uid = self:get_child_uid("equip_bg_img"),
        is_data_driven = true
    })
    self["背景"].binding.img = 120089

    self["图标"] = GT.ui.control.image.obj({
        player = self.player,
        uid = self:get_child_uid("equip_icon_img"),
        is_data_driven = true
    })
    self["图标"].binding.w=92
    self["图标"].binding.h=92

    self["堆叠层数"] = GT.ui.control.label.obj({
        player = self.player,
        uid = self:get_child_uid("equip_stack_label"),
        is_data_driven = true
    })
    self["充能层数"] = GT.ui.control.label.obj({
        player = self.player,
        uid = self:get_child_uid("equip_charge_label"),
        is_data_driven = true
    })
    self["cd"] = GT.ui.control.progress.obj({
        player = self.player,
        uid = self:get_child_uid("equip_cd_progress"),
        is_data_driven = true
    })
    self["禁用"] = GT.ui.control.image.obj({
        player = self.player,
        uid = self:get_child_uid("equip_disabled_img"),
        is_data_driven = true
    })
    self["禁用"].binding.img = 110853

    if data.uid ~= nil then
        if self.item_enter_ck ~= nil then
            self:item_enter(self.item_enter_ck)
        end
        if self.item_leave_ck ~= nil then
            self:item_leave(self.item_leave_ck)
        end
    end
    self:item_watch()
    return self
end

---@param data ui_data_item
---@return GT.ui.control.item
function GT.ui.control.item.obj(data)
    return new(GT.ui.control.item, data)
end

--监听绑定
---@return GT.ui.control.item
function GT.ui.control.item:item_watch()
    ---@type bind_data[]
    local data = {

        {
            key = "slot_binding",
            func = function(val, old)
                self.slot_binding = val
                self:set_ui_unit_slot(self.slot_binding)
            end
        },
        {
            key = "cast_key",
            func = function(val, old)
                self.cast_key = val
                self:set_skill_btn_smart_cast_key(self.cast_key)
            end
        },
        {
            key = "key",
            func = function(val, old)
                self.key = val
                self:set_key(self.key)
            end
        },
    }
    self.bind:add(data)
    return self
end

---进入事件
---@param fun UI_EVENT_item 执行事件
function GT.ui.control.item:item_enter(fun)
    self["背景"]:enter(function(data, player)
        fun(data, player, self)
    end)
end

---离开事件
---@param fun UI_EVENT_item 执行事件
function GT.ui.control.item:item_leave(fun)
    self["背景"]:leave(function(data, player)
        fun(data, player, self)
    end)
end

--设置物品组件绑定单位
---@param field slot_binding 背包槽位类型名
function GT.ui.control.item:set_ui_unit_slot(field)
    game_api.set_ui_comp_unit_slot(self.player.player, self.uid, field.u.unit, field.slot_type, field.slot_index)
end

-- 绑定物品对象到物品组件
---@param  item item 物品对象
---@return GT.ui.control.item
function GT.ui.control.item:set_item_on_ui(item)
    game_api.set_item_on_ui_comp(self.player.player, item, self.uid)
    return self
end

---@param  key integer 快捷键
--设置按钮快捷键
function GT.ui.control.item:set_key(key)
    game_api.set_btn_short_cut(self.player.player, self.uid, key)
end

---设置智能施法快捷键
---@param  key number 快捷键
function GT.ui.control.item:set_skill_btn_smart_cast_key(key)
    game_api.set_skill_btn_smart_cast_key(self.player.player, self.uid, key)
end
